#include "Log.h"

using namespace std;

CLogger::CLogger() :
	logLevel(LOG_ERROR), logMode(MODE_ALL), strtime(new char[24]), buf(
			new char[512])
{
}

CLogger::~CLogger()
{
	if (strtime != NULL)
	{
		delete[] strtime;
		strtime = NULL;
	}

	if (buf != NULL)
	{
		delete[] buf;
		buf = NULL;
	}

	if (fp != NULL)
	{
		fclose(fp);
	}

}

void CLogger::setLogFile(const char *filename)
{
	if (filename != NULL)
	{
		fp = fopen(filename, "a");

		if (fp != NULL)
		{
			chmod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
					| S_IWOTH);
		}
		else
		{
			debug("日志文件打开失败, 请检查是否有存放的目录");
		}

	}
}

void CLogger::setLogLevel(const std::string strlevel)
{
	if (!strlevel.compare("none"))
	{
		printf("log level:none\n");
		logLevel = LOG_NONE;
	}
	else if (!strlevel.compare("debug"))
	{
		printf("log level:debug\n");
		logLevel = LOG_DEBUG;
	}
	else if (!strlevel.compare("info"))
	{
		printf("log level:info\n");
		logLevel = LOG_INFO;
	}
	else if (!strlevel.compare("warn"))
	{
		printf("log level:warn\n");
		logLevel = LOG_WARN;
	}
	else if (!strlevel.compare("error"))
	{
		printf("log level:error\n");
		logLevel = LOG_ERROR;
	}
	else
	{
		printf("log level:default none\n");
		logLevel = LOG_NONE;
	}

	if (logLevel == LOG_NONE)
	{
		if (fp != NULL)
		{
			fclose(fp);
		}

	}
}

void CLogger::setLogMode(const std::string strmode)
{
	if (!strmode.compare("none"))
	{
		printf("log mode:none console\n");
		logMode = MODE_CONSOLE;
	}
	else if (!strmode.compare("console"))
	{
		printf("log mode:console\n");
		logMode = MODE_CONSOLE;
	}
	else if (!strmode.compare("file"))
	{
		printf("log mode:file\n");
		logMode = MODE_CONSOLE;
	}
	else if (!strmode.compare("all"))
	{
		printf("log mode:all\n");
		logMode = MODE_ALL;
	}
	else
	{
		printf("log mode:default console\n");
		logMode = MODE_CONSOLE;
	}
}

// get curr time looks like "17-Jan-2009 14:22:08"
char *CLogger::getTime()
{
	time_t t_now;
	struct tm *tm_now;
	time(&t_now);
	tm_now = localtime(&t_now);
	strftime(strtime, 24, "%Y-%m-%d %H:%M:%S", tm_now);

	return strtime;
}

void CLogger::debug(const char *format, ...)
{
	char *tmp = getTime();

	if ((logLevel == LOG_ERROR || logLevel == LOG_WARN || logLevel == LOG_INFO
			|| logLevel == LOG_DEBUG))
	{
		va_list var_arg;
		va_start(var_arg, format);

		snprintf(buf, 511, "[%s] [debug] %s\n", tmp, format);

		if (logMode == MODE_ALL || logMode == MODE_CONSOLE)
		{
			vprintf(buf, var_arg);
		}

		if (fp != NULL && (logMode == MODE_ALL || logMode == MODE_FILE))
		{
			vfprintf(fp, buf, var_arg);
			fflush(fp);
		}

		va_end(var_arg);
	}
}

void CLogger::info(const char *format, ...)
{
	char *tmp = getTime();

	if (logLevel == LOG_ERROR || logLevel == LOG_WARN || logLevel == LOG_INFO)
	{
		va_list var_arg;
		va_start(var_arg, format);

		snprintf(buf, 511, "[%s] [info] %s\n", tmp, format);

		if (logMode == MODE_ALL || logMode == MODE_CONSOLE)
		{
			vprintf(buf, var_arg);
		}

		if (fp != NULL && (logMode == MODE_ALL || logMode == MODE_FILE))
		{
			vfprintf(fp, buf, var_arg);
			fflush(fp);
		}

		va_end(var_arg);
	}
}

void CLogger::warn(const char *format, ...)
{
	char *tmp = getTime();

	if (logLevel == LOG_ERROR || logLevel == LOG_WARN)
	{
		va_list var_arg;
		va_start(var_arg, format);

		snprintf(buf, 511, "[%s] [warn] %s\n", tmp, format);

		if (logMode == MODE_ALL || logMode == MODE_CONSOLE)
		{
			vprintf(buf, var_arg);
		}

		if (fp != NULL && (logMode == MODE_ALL || logMode == MODE_FILE))
		{
			vfprintf(fp, buf, var_arg);
			fflush(fp);
		}

		va_end(var_arg);
	}
}

void CLogger::error(const char *format, ...)
{
	char *tmp = getTime();

	if (logLevel == LOG_ERROR)
	{
		va_list var_arg;
		va_start(var_arg, format);

		snprintf(buf, 511, "[%s] [error] %s\n", tmp, format);

		if (logMode == MODE_ALL || logMode == MODE_CONSOLE)
		{
			vprintf(buf, var_arg);
		}

		if (fp != NULL && (logMode == MODE_ALL || logMode == MODE_FILE))
		{
			vfprintf(fp, buf, var_arg);
			fflush(fp);
		}

		va_end(var_arg);
	}
}

void CLogger::log(const char *format, ...)
{
	char *tmp = getTime();

	if (logLevel == LOG_ERROR || logLevel == LOG_WARN || logLevel == LOG_INFO
			|| logLevel == LOG_DEBUG)
	{
		va_list var_arg;
		va_start(var_arg, format);

		snprintf(buf, 511, "[%s] [log]  %s\n", tmp, format);

		if (logMode == MODE_ALL || logMode == MODE_CONSOLE)
		{
			vprintf(buf, var_arg);
		}

		if (fp != NULL && (logMode == MODE_ALL || logMode == MODE_FILE))
		{
			vfprintf(fp, buf, var_arg);
			fflush(fp);
		}

		va_end(var_arg);
	}
}

void CLogger::data(const char *format, ...)
{
	char *tmp = getTime();

	if (logLevel == LOG_DATA || LOG_ERROR || logLevel == LOG_WARN || logLevel
			== LOG_INFO || logLevel == LOG_DEBUG)
	{
		va_list var_arg;
		va_start(var_arg, format);

		snprintf(buf, 511, "[%s] [data] %s\n", tmp, format);

		if (logMode == MODE_ALL || logMode == MODE_CONSOLE)
		{
			vprintf(buf, var_arg);
		}

		if (fp != NULL)
		{
			vfprintf(fp, buf, var_arg);
			fflush(fp);
		}

		va_end(var_arg);
	}
}
